package com.alex.server.controller;


import com.alex.server.pojo.Position;
import com.alex.server.pojo.RespBean;
import com.alex.server.service.IPositionService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 职位 前端控制器
 * </p>
 *
 * @author alex
 * @since 2022-08-30
 */
@RestController
@RequestMapping("/system/basic/position")
public class PositionController {
    @Autowired
    private IPositionService iPositionService;
    @ApiOperation(value = "获取所有职位信息")
    @GetMapping("/")
    public List<Position> getAll(){
        //获取所有数据
        List<Position> list = iPositionService.list();
        return list;
    }
    @ApiOperation(value = "添加职位信息")
    @PostMapping("/")
    public RespBean addPo(@RequestBody Position position){
        position.setCreateDate(LocalDateTime.now());
        boolean save = iPositionService.save(position);
        if (save){
            return RespBean.success("添加成功");
        }else {
            return RespBean.error("添加失败");
        }
    }
    @ApiOperation(value = "更新职位信息")
    @PutMapping("/")
    public RespBean update(@RequestBody Position position){
        position.setCreateDate(LocalDateTime.now());
        boolean update = iPositionService.updateById(position);
        if (update){
            return RespBean.success("修改成功");
        }else {
            return RespBean.error("修改失败");
        }
    }
    @ApiOperation(value = "删除职位信息")
    @DeleteMapping("/{id}")
    public RespBean deletePol(@PathVariable Integer id){
        boolean remove = iPositionService.removeById(id);
        if (remove){
            return RespBean.success("删除成功");
        }else {
            return RespBean.error("删除失败");
        }
    }
    @ApiOperation(value = "批量删除职位信息")
    @DeleteMapping("/")
    public RespBean deletePols(Integer[] ids) {
        //批量删除,需要的是集合,而不是数组,需要先把接收到的数组转为集合
        boolean removes = iPositionService.removeByIds(Arrays.asList(ids));
        if (removes) {
            return RespBean.success("删除成功");
        } else {
            return RespBean.error("删除失败");
        }
    }
}
